package com.zust.data.util;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by liy on 2017/1/7.
 */
public class EmailUtil {

    private static JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

    public static void SendRegisterEmail(String email, String md5, Integer id) throws MessagingException {

        // 设定mail server
        senderImpl.setHost("smtp.163.com");

        // 建立邮件消息
//        SimpleMailMessage mailMessage = new SimpleMailMessage();

        // 建立邮件消息,发送简单邮件和html邮件的区别
        MimeMessage mailMessagem = senderImpl.createMimeMessage();
        MimeMessageHelper mailMessage = new MimeMessageHelper(mailMessagem);

        // 设置收件人，寄件人 用数组发送多个邮件
        // String[] array = new String[] {"sun111@163.com","sun222@sohu.com"};
        // mailMessage.setTo(array);
        mailMessage.setTo(email);
        mailMessage.setFrom("senselyan@163.com");
        mailMessage.setSubject("万社-学生社团管理系统");
        mailMessage.setText("<h1>点击如下链接完成注册</h1>" +
                "<p>http://127.0.0.1:8000/activate.html?id="+id+"&secret="+md5+"</p>", true);

        senderImpl.setUsername("senselyan@163.com"); // 根据自己的情况,设置username
        senderImpl.setPassword("wodewangyi1109"); // 根据自己的情况, 设置password

//        Properties prop = new Properties();
        Properties prop = System.getProperties();
        prop.put("mail.smtp.auth", "true"); // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
        prop.put("mail.smtp.timeout", "25000");
        senderImpl.setJavaMailProperties(prop);
        // 发送邮件
        senderImpl.send(mailMessagem);
    }

}
